﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MMSWebsite.Models;
using MMSWebsite.Models.Asset;

namespace MMSWebsite.DataAccessManagement.AssetManagement
{
    public class AttributeAssetProvider
    {
        MMSEntities _entities = new MMSEntities();
        MasterDataAccessProvider _mdap = new MasterDataAccessProvider();
        EmployeeAccessProvider _eap = new EmployeeAccessProvider();

        #region AssetStatus
        public List<AssetStatus> GetStatusByAssetId(int assetId)
        {
            var list = new List<AssetStatus>();

            var statuses = _entities.MMS_ASSET_STATUSes.Where(a => a.AssetId == assetId).ToList();
            foreach (var mmsAssetStatus in statuses)
            {
                list.Add(ConvertMMSStatusToModel(mmsAssetStatus));
            }

            return list;
        }

        public AssetStatus ConvertMMSStatusToModel(MMS_ASSET_STATUS mmsAssetStatus)
        {
            AssetStatus status = null;
            if (mmsAssetStatus.Status != null)
                status = new  AssetStatus
                           {
                               Id = mmsAssetStatus.id, Status = _mdap.GetMDById((int) mmsAssetStatus.Status), StatusDate = mmsAssetStatus.StatusDate
                           };   
            return status;
        }
        #endregion

        #region Valuation
        public List<Valuation> GetValuationByAssetId(int assetId)
        {
            var list = new List<Valuation>();
            var valuations = _entities.MMS_VALUATIONs.Where(v => v.AssetId == assetId).OrderByDescending(x=>x.id);
            foreach (var mmsValuation in valuations)
            {
                list.Add(ConvertMMSValuationToModel(mmsValuation));
            }

            return list;
        }

        public Valuation ConvertMMSValuationToModel(MMS_VALUATION mmsValuation)
        {
            var valuation = new Valuation();

            valuation.Id = mmsValuation.id;
            valuation.ValuationBy = mmsValuation.ValuationBy != null ? _eap.GetEmployeeById(mmsValuation.ValuationBy) : null;
            valuation.ValuationDate = mmsValuation.ValuationDate;
            valuation.Value = mmsValuation.Value;
            return valuation;

        }

        #endregion

        #region Loan

        public List<Loan> GetLoansByAssetId(int assetId)
        {
            
            var list = new List<Loan>();
            var loans = _entities.MMS_LOANs.Where(x => x.AssetId == assetId).OrderByDescending(x => x.id);
            foreach (var mmsLoan in loans)
            {
                list.Add(ConvertMMSLoanToModel(mmsLoan));
            }

            return list;
        }

        public Loan ConvertMMSLoanToModel(MMS_LOAN mmsLoan)
        {
            return new Loan
                       {
                           Id = mmsLoan.id, LoanBy = mmsLoan.LoanBy !=null ? _eap.GetEmployeeById(mmsLoan.LoanBy) : null, 
                           LoanDate = mmsLoan.LoanDate, ValueLoan = mmsLoan.ValueLoan
                       };
        }

        #endregion

        #region Rate

        public List<Rate> GetRatesByAssetId(int assetId)
        {
            var list = new List<Rate>();
            var rates = _entities.MMS_RATEs.Where(r => r.AssetId == assetId).OrderByDescending(x => x.id);
            foreach (var mmsRate in rates)
            {
                 list.Add(ConvertMMSRateToModel(mmsRate));
            }
            return list;
        }

        public Rate ConvertMMSRateToModel(MMS_RATE mmsRate)
        {
            return new Rate
                       {
                           Id = mmsRate.id, RateDate = mmsRate.RateDate, ValueRate = mmsRate.Rate               
                       };
        }

        #endregion
    }
}